Skip to content

Fix missing Microsoft.Extensions.Configuration using in generated ClientSettings when BindCore is customized - #10905

Merged
jorgerangel-msft merged 5 commits into
mainfrom
copilot/fix-generated-clientsettings-compilation
Jun 5, 2026
Merged

Fix missing Microsoft.Extensions.Configuration using in generated ClientSettings when BindCore is customized#10905
jorgerangel-msft merged 5 commits into
mainfrom
copilot/fix-generated-clientsettings-compilation

Conversation

Copilot AI commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

When a generated ClientSettings type's BindCore(IConfigurationSection) is moved to custom code, the generated partial still references IConfigurationSection in its XML doc summary, but using Microsoft.Extensions.Configuration; was dropped — so the generated code no longer compiles.

Root cause: Namespace imports are registered from actual type references during writing. The class description used a plain string <see cref="IConfigurationSection"/> rather than a real type reference, so it never registered the namespace. The only thing registering Microsoft.Extensions.Configuration was the generated BindCore parameter type — once BindCore is customized away and filtered out, nothing registers it.

Changes

  • ClientSettingsProvider.BuildDescription: reference the actual types via the :C cref formatter (matching the ClientOptionsProvider convention) instead of hardcoded <see cref> strings. This registers the Microsoft.Extensions.Configuration namespace while rendering an identical cref, so normal generated output is unchanged.
// before
=> $"Represents the settings used to configure a <see cref=\"{_clientProvider.Name}\"/> that can be loaded from an <see cref=\"IConfigurationSection\"/>.";
// after
=> $"Represents the settings used to configure a {_clientProvider.Type:C} that can be loaded from an {IConfigurationSectionType:C}.";
  • Regression test (TestGeneratedSettings_WithCustomizedBindCore) + TestData: uses TestData to move BindCore into a custom partial, asserts it is no longer generated, and validates the full generated output against an expected snapshot file (TestData/ClientSettingsProviderTests/TestGeneratedSettings_WithCustomizedBindCore.cs), confirming the using is still emitted. Fails without the fix.

… using is emitted

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label Jun 5, 2026

@jorgerangel-msft jorgerangel-msft left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot ensure we add a unit test that uses TestData to move the BindCore method do custom code and then validate that the generated code is generated as expected

…ore customized

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix compilation issue for generated ClientSettings with custom BindCore Fix missing Microsoft.Extensions.Configuration using in generated ClientSettings when BindCore is customized Jun 5, 2026
Copilot AI requested a review from jorgerangel-msft June 5, 2026 18:13
…ore is customized

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>

Copilot AI commented Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

@copilot ensure we add a unit test that uses TestData to move the BindCore method do custom code and then validate that the generated code is generated as expected

Done in 9b9... — TestGeneratedSettings_WithCustomizedBindCore uses TestData to move BindCore into a custom partial, asserts it is no longer generated, and now validates the full generated output against an expected snapshot file (TestData/ClientSettingsProviderTests/TestGeneratedSettings_WithCustomizedBindCore.cs), confirming using Microsoft.Extensions.Configuration; is still emitted.

Comment thread .chronus/changes/fix-clientsettings-configuration-using-2026-6-5-18-0-0.md Outdated
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
@pkg-pr-new

pkg-pr-new Bot commented Jun 5, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-csharp@10905

commit: 913c1da

@jorgerangel-msft
jorgerangel-msft marked this pull request as ready for review June 5, 2026 20:56
@jorgerangel-msft
jorgerangel-msft added this pull request to the merge queue Jun 5, 2026
Merged via the queue into main with commit ab8a8e4 Jun 5, 2026
29 checks passed
@jorgerangel-msft
jorgerangel-msft deleted the copilot/fix-generated-clientsettings-compilation branch June 5, 2026 22:54
JennyPng pushed a commit to JennyPng/typespec that referenced this pull request Jun 10, 2026
…entSettings when BindCore is customized (microsoft#10905)

When a generated `ClientSettings` type's
`BindCore(IConfigurationSection)` is moved to custom code, the generated
partial still references `IConfigurationSection` in its XML doc summary,
but `using Microsoft.Extensions.Configuration;` was dropped — so the
generated code no longer compiles.

**Root cause:** Namespace imports are registered from actual type
references during writing. The class description used a plain string
`<see cref="IConfigurationSection"/>` rather than a real type reference,
so it never registered the namespace. The only thing registering
`Microsoft.Extensions.Configuration` was the generated `BindCore`
parameter type — once `BindCore` is customized away and filtered out,
nothing registers it.

### Changes
- **`ClientSettingsProvider.BuildDescription`**: reference the actual
types via the `:C` cref formatter (matching the `ClientOptionsProvider`
convention) instead of hardcoded `<see cref>` strings. This registers
the `Microsoft.Extensions.Configuration` namespace while rendering an
identical cref, so normal generated output is unchanged.

```csharp
// before
=> $"Represents the settings used to configure a <see cref=\"{_clientProvider.Name}\"/> that can be loaded from an <see cref=\"IConfigurationSection\"/>.";
// after
=> $"Represents the settings used to configure a {_clientProvider.Type:C} that can be loaded from an {IConfigurationSectionType:C}.";
```

- **Regression test** (`TestGeneratedSettings_WithCustomizedBindCore`) +
TestData: uses TestData to move `BindCore` into a custom partial,
asserts it is no longer generated, and validates the full generated
output against an expected snapshot file
(`TestData/ClientSettingsProviderTests/TestGeneratedSettings_WithCustomizedBindCore.cs`),
confirming the `using` is still emitted. Fails without the fix.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generated ClientSettings Don't Compile if BindCore is Customized

3 participants